package com.bree.erp.jsf;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.bree.erp.entity.Group;
import com.bree.erp.entity.GroupType;
import com.bree.erp.entity.ItemRequest;
import com.bree.erp.entity.Project;
import com.bree.erp.entity.PurchaseRequisition;
import com.bree.erp.entity.Status;
import com.bree.erp.entity.User;
import com.bree.erp.service.GroupService;
import com.bree.erp.service.ItemRequestService;
import com.bree.erp.service.ProjectService;
import com.bree.erp.service.PurchaseRequisitionService;
import com.bree.erp.service.UserService;

@Deprecated
public class PurchaseRequisitionDetailBean extends PurchaseRequisition {

	private static Logger LOG = Logger
			.getLogger(PurchaseRequisitionDetailBean.class);

	@EJB
	private ProjectService projectService;

	@EJB
	private UserService userService;

	@EJB
	private GroupService groupService;

	@EJB
	private PurchaseRequisitionService prService;

	@EJB
	private ItemRequestService irService;

	private AuthUserBean authUserBean;
	// private ItemRequestListBean itemRequestListBean;

	private String projectCode;
	private long projectId;
	private String approverId;

	private UIComponent approverDropDown;

	public List<Project> getProjects() {

		return projectService.list();
	}

	private PurchaseRequisition getSessionPurchaseRequisition() {
		FacesContext context = FacesContext.getCurrentInstance();

		PurchaseRequisition session = (PurchaseRequisition) context
				.getApplication()
				.evaluateExpressionGet(context, "#{purchaseRequisitionBean}",
						PurchaseRequisition.class);

		return session;
	}
	
	private void updateApproverParameter(PurchaseRequisition src) {
		this.setApproverId(src.getApprovedBy() == null ? null : src.getApprovedBy().getEmail());
	}

	@PostConstruct
	public void init() {

		LOG.debug("@PostConstruct: PurchaseRequisitionDetailBean");

		PurchaseRequisition session = getSessionPurchaseRequisition();

		copy(session, this);
		updateApproverParameter(session);
	}

	public void save() {

		long id = this.getId();
		Project project = projectService.read(projectId);
		String description = this.getDescription();

		User approver = approverId == null ? null : userService
				.read(approverId);

		PurchaseRequisition updated = prService.update(id, project,
				description, approver);

		copy(updated, this);
		updateApproverParameter(updated);
		

		// update session
		PurchaseRequisition session = getSessionPurchaseRequisition();
		copy(updated, session);

	}
	
	public boolean getRenderSave() {
		PurchaseRequisition session = getSessionPurchaseRequisition();
		
		if(session.getStatus() == Status.OPEN) {
			return true;
		}
		
		return false;
	}

	public String submit() {

		User approver = approverId == null ? null : userService
				.read(approverId);

		if (approver == null) {
			FacesMessage message = new FacesMessage(
					"Approver is mandatory during PR submission.");
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(approverDropDown.getClientId(context), message);
			return null;
		}

		long id = this.getId();
		Project project = projectService.read(projectId);
		String description = this.getDescription();
		
		
		prService.update(id, project, description, approver);
		PurchaseRequisition updated = prService.submit(id);
		
		//copy(updated, this);

		// update session
		PurchaseRequisition session = getSessionPurchaseRequisition();
		copy(updated, session);
		updateApproverParameter(updated);
		
		return "back";
	}
	
	public boolean getRenderSubmit() {
		PurchaseRequisition session = getSessionPurchaseRequisition();
		
		if(session.getStatus() == Status.OPEN) {
			return true;
		}
		
		return false;
	}
	
	public boolean getRenderWithdraw() {

		PurchaseRequisition session = getSessionPurchaseRequisition();
		
		if(session.getStatus() == Status.PENDING 
				&& authUserBean.getLoggedInUser().equals(session.getCreatedBy())) {
			return true;
		}
		
		return false;
		
	}

	public List<User> getApprovers() {
		Collection<Group> groups = groupService
				.findByGroupType(GroupType.PRAPPROVER);

		List<User> approvers = new ArrayList<User>();

		if (groups != null) {
			for (Group group : groups) {
				approvers.add(group.getUser());
			}
		}

		return approvers;
	}

	public List<ItemRequest> getItemRequests() {

		List<ItemRequest> list = new ArrayList<ItemRequest>();

		if (this.getRequests() != null) {
			list.addAll(this.getRequests());
		}

		return list;
	}

	public void deleteRequestItem(long id) {

		LOG.debug(this.getId() + ": deleteRequestItem(" + id + ")");

		if (this.getId() == 0) {

			LOG.debug("Resolving PR-ID from child RI.");

			ItemRequest req = irService.read(id);

			setId(req.getPurchaseRequisition().getId());
		}

		irService.delete(id);

		PurchaseRequisition updated = prService.read(getId());

		copy(updated, this);

	}

	/**
	 * @return the projectCode
	 */
	public String getProjectCode() {
		return projectCode;
	}

	/**
	 * @param projectCode
	 *            the projectCode to set
	 */
	public void setProjectCode(String projectCode) {
		this.projectCode = projectCode;
	}

	/**
	 * @return the authUserBean
	 */
	public AuthUserBean getAuthUserBean() {
		return authUserBean;
	}

	/**
	 * @param authUserBean
	 *            the authUserBean to set
	 */
	public void setAuthUserBean(AuthUserBean authUserBean) {
		this.authUserBean = authUserBean;
	}

	/**
	 * @return the projectId
	 */
	public long getProjectId() {
		return projectId;
	}

	/**
	 * @param projectId
	 *            the projectId to set
	 */
	public void setProjectId(long projectId) {
		this.projectId = projectId;
	}

	/**
	 * @return the approverId
	 */
	public String getApproverId() {
		return approverId;
	}

	/**
	 * @param approverId
	 *            the approverId to set
	 */
	public void setApproverId(String approverId) {
		this.approverId = approverId;
	}

	/**
	 * @return the approverDropDown
	 */
	public UIComponent getApproverDropDown() {
		return approverDropDown;
	}

	/**
	 * @param approverDropDown
	 *            the approverDropDown to set
	 */
	public void setApproverDropDown(UIComponent approverDropDown) {
		this.approverDropDown = approverDropDown;
	}

	/**
	 * @return the itemRequestListBean
	 */
	// public ItemRequestListBean getItemRequestListBean() {
	// return itemRequestListBean;
	// }

	/**
	 * @param itemRequestListBean
	 *            the itemRequestListBean to set
	 */
	// public void setItemRequestListBean(ItemRequestListBean
	// itemRequestListBean) {
	// this.itemRequestListBean = itemRequestListBean;
	// }

}
